var Database = {
    server: null, // changed on Select()
    Select: function( selectbox, server ) {
        var index = selectbox.selectedIndex;
        var database = selectbox.options[ index ].value;
        
        this.server = server;

        if ( database == '##new##' ) {
            // create database
            this.GoCreate( selectbox.parentNode.getElementsByTagName( 'div' )[ 0 ] );
        }
        else {
            // select database
            this.Use( database );
        }
    },
    Use: function( name, server ) {
        if ( server == null ) {
            server = this.server;
        }

        window.location = '?server=' + server + '&database=' + name;
    },
    GoCreate: function( which ) {
        var node = which.cloneNode( true );
        node.style.display = '';
        Modals.Create( node, 300, 100 );
    },
    DoCreate: function( name ) {
        Coala.Warm( "db/create", { "name": name, "callback": this.CreateCallback } );
    },
    CreateCallback: function( name, success, errmsg ) {
        if ( success ) {
            Database.Use( name );
        }
        else {
            // TODO: Do something better here
            alert( errmsg );
        }
    }
};
